<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      label-width="100px"
      ref="form"
      class="kylin-form"
    >
      <el-form-item :label="$t('system.ip_address')" prop="host">
        <virtual-list
            :value="form.host"
            size="small"
            class="size"
            :list="hosts"
            :multiple="true"
            :obj="true"
            @selectValue="handleSelectIp"
          ></virtual-list>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">{{$t("cancel")}}</el-button>
      <el-button class="kylin-button" @click="handleOk">{{$t("confirm")}}</el-button>
    </div>
  </div>
</template>

<script>
import virtualList from "@/components/virtualList";
import { getAgentList } from "@/api/system"
import { addHost } from "@/api/scan"
export default {
  components: {
      virtualList
  },
  data() {
    return {
      hosts: [],
      form: {
        host: []
      },
      rules: {
        host: [
          {
            required: true,
            message: this.$t("system.ip_require"),
            trigger: "change",
          },
        ]
      },
    };
  },
  mounted() {
      getAgentList({paged: false}).then(res => {
          let hosts = [];
          res.data.data.results.forEach(item => {
              hosts.push({
                  id: item.ip,
                  name: item.ip
              })
          })
          this.hosts = hosts;
      })
  },
  methods: {
    handleCancel() {
      this.$emit("click");
    },
    handleOk() {
      addHost(this.form.host).then(res => {
          if (res.data.code == "0") {
              this.$message({
                  type: "success",
                  message: res.data.message
              })
              this.$emit("click", "success")
          } else {
              this.$message({
                  type: "error",
                  message: res.data.message
              })
          }
      })
    },
   handleSelectIp(value) {
       this.form.host = value;
   }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.size {
      width: 100%;
}
</style>